/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.shiep.aurora.calendar;

import com.sun.awt.AWTUtilities;
import java.awt.Component;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * 一个实现了无窗体带鼠标拖拽移动的窗口
 *
 * @author Eric
 */
public class BaseFrame extends JFrame {
    JPanel jp;
    public BaseFrame(JPanel jp) {
        super();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setUndecorated(true);
        AWTUtilities.setWindowOpaque(this, false);
        add(jp);
        this.addMouseListener(moveWindowListener);
        this.addMouseMotionListener(moveWindowListener);
        this.jp=jp;
        this.setAlwaysOnTop(true);//设置窗体总在最顶层
    }
    public void addMoreListener(){
                jp.addMouseListener(moveWindowListener);
        jp.addMouseMotionListener(moveWindowListener);
        for (Component c : jp.getComponents()) {
            c.addMouseListener(moveWindowListener);
            c.addMouseMotionListener(moveWindowListener);
        }
    }
    private MouseAdapter moveWindowListener = new MouseAdapter() {
        private Point lastPoint = null;

        @Override
        public void mousePressed(MouseEvent e) {
            lastPoint = e.getLocationOnScreen();
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            Point point = e.getLocationOnScreen();
            int offsetX = point.x - lastPoint.x;
            int offsetY = point.y - lastPoint.y;
            Rectangle bounds = BaseFrame.this.getBounds();
            bounds.x += offsetX;
            bounds.y += offsetY;
            BaseFrame.this.setBounds(bounds);
            lastPoint = point;
        }
    };
}
